﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SisLegal.Model;
using System.IO;

namespace SisLegal.Conciliation
{
    public partial class ConciliationUploadForm : System.Web.UI.Page
    {
        private int ID
        {
            get
            {
                if (Request.QueryString["id"] == null)
                    return 0;
                else
                    return int.Parse(Request.QueryString["id"]);
            }
        }

        private int Type
        {
            get
            {
                if (Request.QueryString["type"] == null)
                    return 0;
                else
                    return int.Parse(Request.QueryString["type"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ConciliationManager fiscalFolder = new ConciliationManager();
                Model.Conciliation fiscal = fiscalFolder.GetById(ID);
                txtNroExpedient.Text = fiscal.NroConciliation;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                try
                {
                    FileLoad file = new FileLoad();
                    String guid = Guid.NewGuid().ToString();
                    string filename = Path.GetFileName(FileUpload1.FileName);
                    file.Name = filename;
                    file.Guid = guid;
                    file.Ext = Path.GetExtension(filename);
                    file.IDRef = ID;
                    file.Type = Type;

                    FileUpload1.SaveAs(Server.MapPath("~/Uploads/") + guid);
                    FileLoadManager manager = new FileLoadManager();
                    manager.Save(file);
                    Response.Redirect("/Conciliation/ConciliationDisplayForm.aspx?id=" + ID);
                    //StatusLabel.Text = "Upload status: File uploaded!";
                }
                catch (Exception ex)
                {
                    //StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
        }
    }
}